using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class SequenceProductionSpecification {
        [Test]
        public void ShouldParseIfAllProductionsParsedInSequence () {
            var context1 = new ParseContext();
            var context2 = new ParseContext();
            var context3 = new ParseContext();
            var parseResult = new ParseResult(5, null, context3);

            var prod1 = new IndexAndContextExpectingProduction(new ParseResult(2, null, context2), 0, context1);
            var prod2 = new IndexAndContextExpectingProduction(parseResult, 2, context2);

            var seq = new SequenceProduction (prod1, prod2);

            string sourceString = "a b c";
            var source = sourceString.ToCharArray ();

            var result = seq.Parse (source, 0, context1, sourceString, new ParseEnvironment (null));
            Assert.AreSame (parseResult, result);
        }

        [Test]
        public void ShouldNotParseIfSecondProductionFails () {
            var prod1 = new Mock<IProduction> ();
            var prod2 = new Mock<IProduction> ();

            var seq = new SequenceProduction (prod1.Object, prod2.Object);

            var context = new ParseContext ();
            string sourceString = "a b c";
            var source = sourceString.ToCharArray ();

            prod1.Setup (
                p => p.Parse (source, 0, context, sourceString, It.IsAny<ParseEnvironment> ())).Returns (new ParseResult (2, null, context));

            var result = seq.Parse (source, 0, context, sourceString, new ParseEnvironment (null));
            Assert.IsNull (result);
        }
    }
}